//
//  NearbyPlacesViewController.m
//  HCWeibo
//
//  Created by gj on 15/12/18.
//  Copyright © 2015年 www.iphonetrain.com 无限互联. All rights reserved.
//

#import "NearbyPlacesViewController.h"
#import "Common.h"
#import "NetworkService.h"
#import "PoiModel.h"
#import "UIImageView+WebCache.h"

@implementation NearbyPlacesViewController{
    UITableView *_tableView;
    NSMutableArray *_dataArray;


}


- (void)viewDidLoad{
    [super viewDidLoad];
    self.title = @"附近地点";
    self.view.backgroundColor = [UIColor whiteColor];
    //01 创建tableView
    [self _createTableView];
    //02 定位
    [self _location];
}

#pragma mark - tableView


- (void)_createTableView{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    //注册单元格
    [_tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    
    [self.view addSubview:_tableView];


}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataArray.count;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
//    if (cell == nil) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
//    }
//    
//
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    PoiModel *model = _dataArray[indexPath.row];
    cell.textLabel.text = model.title;
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.icon] placeholderImage:[UIImage imageNamed:@"icon"]];

    
    return cell;

}




#pragma mark - 定位
- (void)_location{
    
    if (_locationManager == nil) {
        //创建位置管理器对象
        _locationManager = [[CLLocationManager alloc] init];
        
        if (ios8) {
            //权限请求
            [_locationManager requestWhenInUseAuthorization];
        }

    }
    //精度设置
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    
    //设置代理
    _locationManager.delegate = self;
    //开始更新位置
    [_locationManager startUpdatingLocation];
    

}

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
    
    // 停止定位
    [manager stopUpdatingLocation];
    
    // 获取当前请求的位置
    CLLocation *location = [locations lastObject];
    
    NSString *lon = [NSString stringWithFormat:@"%f",location.coordinate.longitude];
    NSString *lat = [NSString stringWithFormat:@"%f",location.coordinate.latitude];
    // 开始加载网络
    [self loadNearByPoisWithlon:lon lat:lat];
    

}





#pragma mark - 网络获取附近地点
- (void)loadNearByPoisWithlon:(NSString *)lon lat:(NSString *)lat
{
    
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:lon forKey:@"long"];
    [params setObject:lat forKey:@"lat"];
    [params setObject:@"50" forKey:@"count"];
    
    
    [NetworkService requestAFUrl:nearby_pois httpMethod:@"GET" params:params data:nil block:^(id result, NSURLResponse *response, NSError *error) {
        NSArray *pois = [result objectForKey:@"pois"];
        NSMutableArray *modelArray = [[NSMutableArray alloc] init];
        for (NSDictionary *dic in pois) {
            
            PoiModel *model = [[PoiModel alloc] initWithDataDic:dic];
            [modelArray addObject:model];
        }
        _dataArray = modelArray;
        [_tableView reloadData];
        
    
    }];
    
    
}













#if 0 //学习演示用
//代理获取定位数据
- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray<CLLocation *> *)locations{
//    typedef struct {
//        CLLocationDegrees latitude;
//        CLLocationDegrees longitude;
//    } CLLocationCoordinate2D;
    
    //01 经纬度获取
    //停止位置更新
    [_locationManager stopUpdatingLocation];
    
    CLLocation *location = [locations lastObject];
    //得到坐标
    CLLocationCoordinate2D coordinate =  location.coordinate;
    
    NSLog(@"经度%lf,纬度%lf",coordinate.longitude,coordinate.latitude);
    
    
    //02 地理位置反编码
    
   //方式一: 微博接口 http://open.weibo.com/wiki/2/location/geo/geo_to_address
    
    NSString *cooridnateStr = [NSString stringWithFormat:@"%f,%f",coordinate.longitude,coordinate.latitude];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    
    [params setObject:cooridnateStr forKey:@"coordinate"];
    
//    [NetworkService requestDataWithURL:geo_to_address HTTPMethod:@"GET" params:params completionHandle:^(id result, NSURLResponse *response, NSError *error) {
//       
//        NSLog(@"%@",result);
//        NSArray *geos = result[@"geos"];
//        if (geos.count > 0) {
//            NSDictionary *dic = [geos lastObject];
//            
//            NSString *addrs = dic[@"address"];
//            NSLog(@"%@",addrs);
//            
//        }
//        
//    }];
//
  
//    [NetworkService requestAFUrl:geo_to_address httpMethod:@"GET" params:params data:nil block:^(id result, NSURLResponse *response, NSError *error) {
//        
//                NSArray *geos = result[@"geos"];
//                if (geos.count > 0) {
//                    NSDictionary *dic = [geos lastObject];
//        
//                    NSString *addrs = dic[@"address"];
//                    NSLog(@"%@",addrs);
//                    
//                }
//    }];
//
    
    
    //二 官方接口 反编码
    CLGeocoder *geoCoder = [[CLGeocoder alloc] init];
    
    [geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        
        CLPlacemark *place = [placemarks lastObject];
        
        
        NSLog(@"%@",place.name);
        
    
    }];
    
    
}
#endif

@end
